//
//  WLMPictureCell.swift
//  WeiBo
//
//  Created by Eric on 2017-01-09.
//  Copyright © 2017 Eric. All rights reserved.
//

import UIKit
import YYWebImage

class WLMPictureCell: UICollectionViewCell {
    
    lazy var iconView: YYAnimatedImageView = {
        let icon = YYAnimatedImageView()
        icon.contentMode = .scaleAspectFill
        icon.clipsToBounds = true
        return icon
    }()
    
    lazy var gifIcon: UIImageView = {
        let icon = UIImageView()
        icon.image = #imageLiteral(resourceName: "timeline_image_gif")
        return icon
    }()
    
    var pictureInfo: WLMPictureInfo? {
        didSet{
            let url = URL(string: pictureInfo?.bmiddle ?? "")
            let urlString = url?.absoluteString ?? ""
            iconView.yy_setImage(with: url, placeholder: #imageLiteral(resourceName: "timeline_image_placeholder"))
            gifIcon.isHidden = !urlString.hasSuffix(".gif")
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        self.contentView.addSubview(iconView)
        self.contentView.addSubview(gifIcon)
        iconView.snp.makeConstraints { (make) in
            make.edges.equalTo(self.contentView)
        }
        gifIcon.snp.makeConstraints { (make) in
            make.right.bottom.equalTo(self.iconView)
            make.width.equalTo(20)
            make.height.equalTo(15)
        }

    }
}
